<?php

require_once dirname(__FILE__).'/../lib/machineGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/machineGeneratorHelper.class.php';

/**
 * machine actions.
 *
 * @package    maslogger
 * @subpackage machine
 * @author     Michaël VEROUX
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class machineActions extends autoMachineActions
{
     public function executeAjaxEdit(sfWebRequest $request)
     {
         $this->getResponse()->setContentType('application/json');

         $string = $request->getParameter('q');

         $req = Doctrine::getTable('MlMachines')->getAjaxSearch($string);

         $results = array();
         foreach ( $req as $result )
         $results[$result->getId()] = 'Machine n°'.$result->getNumero().' - socle n°'.$result->getSocle();
         
         if(count($results)<=0){
         	$results[0] = 'Aucun résultat !';
         }

         return $this->renderText(json_encode($results));
     }
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));
	try{
	    if ($this->getRoute()->getObject()->hardDelete())
	    {
	      $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
	    }
	}
	catch(Exception $e){
		$data = $this->getRoute()->getObject();
    	$this->getUser()->setFlash('error', 'La machine '.$data->getNumero().' est utilisée ! Impossible de l\'effacer.');
    }
    $this->redirect('/machine/new');
  }	
}
